package io.github.weiranyi.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    public static final String EXCHANGE_NAME = "boot_topic_exchange";
    public static final String QUEUE_NAME1 = "boot_queue1";
    public static final String QUEUE_NAME2 = "boot_queue2";
    
    // 1.交换机
    @Bean("bootExchange")
    public Exchange bootExchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }
    //2.Queue 队列
    @Bean("bootQueue1") // bean name
    public Queue bootQueue1(){
        return QueueBuilder.durable(QUEUE_NAME1).build();
    }
    @Bean
    public Queue bootQueue2(){
        return QueueBuilder.durable(QUEUE_NAME1).build();
    }
    //3. 队列和交互机绑定关系 Binding
    /*
        1. 知道哪个队列
        2. 知道哪个交换机
        3. routing key
        noargs()：表示不指定参数
     */
    @Bean
    public Binding bindQueueExchange1(@Qualifier("bootQueue1") Queue queue1,
                                     @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue1).to(exchange).with("boot1.#").noargs();
    }
    @Bean
    public Binding bindQueueExchange2(@Qualifier("bootQueue2") Queue queue2,
                                      @Qualifier("bootExchange") Exchange exchange){
        return BindingBuilder.bind(queue2).to(exchange).with("boot2.#").noargs();
    }
}
